(function ($) {
	Drupal.behaviors.tbnd_login = {
		attach: function(context, settings) { 	
            $(".show_register").live("click" , function(){		
                $('#tbnd_form_register').css('display' , 'block');
                $('.title-bar h2').text('Đăng kí tài khoản');
                $('#tbnd_form_login , #tbnd_form_recover').css('display' , 'none');
            });
            $(".show_login").live("click" , function(){			
                $('#tbnd_form_register , #tbnd_form_recover').css('display' , 'none');
                $('#tbnd_form_login').css('display' , 'block');
                $('.title-bar h2').text('Đăng nhập');
            });
            $('.show_recover').live("click" , function(){			
                $('#tbnd_form_register , #tbnd_form_login').css('display' , 'none');
                $('#tbnd_form_recover').css('display' , 'block');
                $('.title-bar h2').text('Lấy lại mật khẩu');
            });
            
            $('.show_login').live("click" , function(){			
               fblogin();
            });
            
            $("#btn_login_google").live("click" , function(){
                screenY    = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
                outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
                outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
                width    = 800,
                height   = 370,
                left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
                top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
                features = (
                   'width=' + width +
                   ',height=' + height +
                   ',left=' + left +
                   ',top=' + top
                 );
                var popupWidth=500;
                var popupHeight=300;
                var xPosition=(jQuery(window).width()-popupWidth)/2;
                var yPosition=(jQuery(window).height()-popupHeight)/2;
                var loginUrl = Drupal.settings.basePath  + 'google/auth';

                googleLoginWindow=window.open(loginUrl , "LoginWindow", 
                    "location=1,scrollbars=1,"+
                    "width="+popupWidth+",height="+popupHeight+","+
                    "left="+xPosition+",top="+yPosition);

                //loginWindowTimer=setInterval(onTimerCallbackToCheckLoginWindowClosure, 1000);
                //window.open( Drupal.settings.basePath  + 'google/auth', '','height=450,width=700');
            });
            
            $("#btn_login_facebook").live("click" , function(){			
                screenY    = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
                outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
                outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
                width    = 500,
                height   = 270,
                left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
                top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
                features = (
                   'width=' + width +
                   ',height=' + height +
                   ',left=' + left +
                   ',top=' + top
                 );
                var popupWidth=500;
                var popupHeight=300;
                var xPosition=(jQuery(window).width()-popupWidth)/2;
                var yPosition=(jQuery(window).height()-popupHeight)/2;
                /*var loginUrl="http://www.facebook.com/dialog/oauth/?"+
                    "scope=publish_stream&"+
                    "client_id="+152574051616341+"&"+
                    "redirect_uri="+ "http://thuebannhadat.vn" + "&"+
                    "response_type=token&"+
                    "display=popup";*/
                var loginUrl = Drupal.settings.basePath  + 'facebook/auth';
                facebookLoginWindow=window.open(loginUrl , "LoginWindow", 
                    "location=1,scrollbars=1,"+
                    "width="+popupWidth+",height="+popupHeight+","+
                    "left="+xPosition+",top="+yPosition);

                loginWindowTimer=setInterval(onTimerCallbackToCheckLoginWindowClosure, 1000);

                window.opener.location.reload();
            });
            
            /*--------------------event_click_login-----------------------*/
            $(".btn-sign-in").live("click" , function(){	
                var list_error = []; 
                /*---------------check_email-----------------*/
                var email = $('.txt_signin_email').val();
                var reg_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(!email){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signin_email' 
					};
					list_error.push( a_error );					
				}
                else if(reg_email.test(email) === false){
                    var a_error = {
						text_error : 'Email này không hợp lệ.' ,
						class : 'txt_signin_email' 
					};
					list_error.push( a_error );		
                }
                var password = $('.txt_signin_password').val();
                if(!password){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signin_password' 
					};
					list_error.push( a_error );					
				}
               
                if(list_error.length > 0){			
					$("#form_login input").removeClass('form_error');		
                    $("#form_login .alert").hide();		
                    $.each(list_error , function (index , value){ 
                        $('#form_login ' + '.' + value.class).addClass('form_error');
                        $('#form_login ' + '.'+value.class+'_error .msg').html(value.text_error);
                        $('#form_login ' + '.'+value.class+'_error').show()
                    });          
                    $('.'+list_error[0].class).focus();                                     
                    return false;                       
                }
                else{
                    //$("#form_login input").removeClass('form_error');		
                    //$("#form_login .alert").hide();	
                }
                 //check_none_error
                me = $(this);
                $.ajax({
                    cache: false,
                    type: 'post',
                    url: Drupal.settings.basePath + 'ajax/login',
                    data: $('#form_login').serialize(),
                    dataType: 'json',
                    beforeSend: function(){
						me.removeClass('btn-sign-in');
						$('#form_login .ajax_loading').show();
						me.attr('disabled', 'disabled');
						me.addClass('disabled');                       
                    },
                    success: function( json ) {                        
                        if(json.status == true){
                            $("#form_login input").removeClass('form_error');		
                            $("#form_login .alert").hide();	
                            window.location.reload();
                        }
                        else{
                            $("#form_login input").removeClass('form_error');		
                            $("#form_login .alert").hide();		
                            $.each(json.list_error , function(index, value) {
                                $('#form_login ' + '.' + value.class).addClass('form_error');
                                $('#form_login ' + '.'+value.class+'_error .msg').html(value.text_error);
                                $('#form_login ' + '.'+value.class+'_error').show();
                            });
                            
                            /*reset_button*/
                            me.addClass('btn-sign-in');
                            $('#form_login .ajax_loading').hide();
                            me.removeAttr('disabled');
                            me.removeClass('disabled');        
                            /*------------*/
                        }						
                    }
                });
				return false;
                
            });
            /*--------------------end_event_click_login-----------------------*/
            
             /*--------------------event_click_recover_password-----------------------*/
            $(".btn-recover-password").live("click" , function(){	
                var list_error = []; 
                /*-------------check_email_recover----------------------------*/
                var email = $('.txt_recover_email').val();
                var reg_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                if(!email){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_recover_email' 
					};
					list_error.push( a_error );
				}         
                else if(reg_email.test(email) === false){
                    var a_error = {
						text_error : 'Email này không hợp lệ.' ,
						class : 'txt_recover_email' 
					};
					list_error.push( a_error );		
                }
               /*-------------check_email_recover----------------------------*/
               if(list_error.length > 0){			
					$("#form_recover input").removeClass('form_error');		
                    $("#form_recover .alert").hide();		
                    $.each(list_error , function (index , value){ 
                        $('#form_recover ' + '.'+value.class).addClass('form_error');
                        $('#form_recover ' + '.'+value.class+'_error .msg').html(value.text_error);
                        $('#form_recover ' + '.'+value.class+'_error').show()
                    });          
                    $('.'+list_error[0].class).focus();                                     
                    return false;                       
                }
                
                  //check_none_error
                me = $(this);
                $.ajax({
                    cache: false,
                    type: 'post',
                    url: Drupal.settings.basePath + 'ajax/recover_pass',
                    data: $('#form_recover').serialize(),
                    dataType: 'json',
                    beforeSend: function(){
                        me.removeClass('btn-recover-password');
						$('#form_recover .ajax_loading').show();
						me.attr('disabled', 'disabled');
						me.addClass('disabled');       
                    },
                    success: function( json ) {
                        if(json.status == true){
							var html_success = '<p class="fwb">Bạn đã lấy lại mật khẩu thành công</p>';
                            html_success += '<p>Hệ thống thuebannhadat.vn đã gửi vào hòm thư</p>';
                            html_success += '<p class="fwb color_dd4b39 ">' + $('.txt_recover_email').val() + '</p>';
                            html_success += '<p>Bạn vui lòng kiểm tra email để nhận mật khẩu mới</p>';                            
                            $('#form_recover table tbody').html( '<tr><td>' + html_success + '</td></tr>');                            
                        }
                        else{
                            $("#form_recover input").removeClass('form_error');		
                            $("#form_recover .alert").hide();		
                            $.each(json.list_error , function(index, value) {
                                $('#form_recover ' + '.' + value.class).addClass('form_error');
                                $('#form_recover ' + '.'+value.class+'_error .msg').html(value.text_error);
                                $('#form_recover ' + '.'+value.class+'_error').show();
                            });
                            
                            /*reset_button*/
                            me.addClass('btn-recover-password');
                            $('#form_recover .ajax_loading').hide();
                            me.removeAttr('disabled');
                            me.removeClass('disabled');        
                            /*------------*/
                        }
                    }
                });
                return false;
            });
            /*--------------------end_event_click_singin-----------------------*/
            
            /*--------------------event_click_singin-----------------------*/
            $(".btn-sign-up").live("click" , function(){		
                var list_error = []; 
                /*---------------check_name-----------------*/
				var full_name = $.trim($('.txt_signup_fullname').val());
				if(!full_name){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signup_fullname' 
					};
					list_error.push( a_error );
					
				}
                /*---------------check_email-----------------*/
                var email = $('.txt_signup_email').val();
                var reg_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(!email){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signup_email' 
					};
					list_error.push( a_error );
					
				}
                 else if(reg_email.test(email) === false){
                    var a_error = {
						text_error : 'Email này không hợp lệ.' ,
						class : 'txt_signup_email' 
					};
					list_error.push( a_error );		
                }
                
                 /*---------------check_password-----------------*/
                var password = $('.txt_signup_password').val();
				if(!password){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signup_password' 
					};
					list_error.push( a_error );
					
				}
                /*---------------check_password_confirm-----------------*/
                var password_confirm = $('.txt_signup_password_confirm').val();
				if(!password_confirm){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signup_password_confirm' 
					};
					list_error.push( a_error );
					
				}
                
                if(password != password_confirm){
                    var a_error = {
						text_error : 'Mật khẩu không khớp.' ,
						class : 'txt_signup_password' 
					};
					list_error.push( a_error );
                    var a_error = {
						text_error : 'Mật khẩu không khớp.' ,
						class : 'txt_signup_password_confirm' 
					};
					list_error.push( a_error );
                }
                
                 /*---------------check_phone-----------------*/
                var password = $('.txt_signup_phone').val();
				if(!password){					
					var a_error = {
						text_error : 'Thông tin này không được trống.' ,
						class : 'txt_signup_phone' 
					};
					list_error.push( a_error );
					
				}
                
                if(list_error.length > 0){			
					$("#form_register input").removeClass('form_error');		
                    $("#form_register .alert").hide();		
                    $.each(list_error , function (index , value){ 
                        $('.'+value.class).addClass('form_error');
                        $('.'+value.class+'_error .msg').html(value.text_error);
                        $('.'+value.class+'_error').show()
                    });          
                    $('.'+list_error[0].class).focus();                                     
                    return false;                       
                }
                else{
                    //$("#form_register input").removeClass('form_error');		
                    //$("#form_register .alert").hide();	
                }
                //check_none_error
                me = $(this);
                $.ajax({
                    cache: false,
                    type: 'post',
                    url: Drupal.settings.basePath + 'ajax/register',
                    data: $('#form_register').serialize(),
                    dataType: 'json',
                    beforeSend: function(){
                        me.removeClass('btn-sign-up');
						$('#form_register .ajax_loading').show();
						me.attr('disabled', 'disabled');
						me.addClass('disabled');       
                    },
                    success: function( json ) {
                        if(json.status == true){
                            //window.location.reload();
                            var html_success = '<p class="fwb">Bạn đã tạo tài khoản thành công</p>';
                            html_success += '<p>Hệ thống thuebannhadat.vn đã gửi vào hòm thư</p>';
                            html_success += '<p class="fwb color_dd4b39 ">' + $('.txt_signup_email').val() + '</p>';
                            html_success += '<p>Bạn vui lòng kiểm tra email và làm theo hướng dẫn để hoàn tất quá trình đăng kí</p>';
                            
                            $('#form_register table tbody').html( '<tr><td>' + html_success + '</td></tr>');
                        }
                        else{
                            $("#form_register input").removeClass('form_error');		
                            $("#form_register .alert").hide();		
                            $.each(json.list_error , function(index, value) {
                                $('#form_register ' + '.' + value.class).addClass('form_error');
                                $('#form_register ' + '.'+value.class+'_error .msg').html(value.text_error);
                                $('#form_register ' + '.'+value.class+'_error').show();
                            });
                            
                            /*reset_button*/
                            me.addClass('btn-sign-up');
                            $('#form_register .ajax_loading').hide();
                            me.removeAttr('disabled');
                            me.removeClass('disabled');        
                            /*------------*/
                        }
						//$('.pr_ajax_loading').hide();                        
                        //$('.msg_error').text(res.msg).fadeIn('slow');
                    }
                });
				return false;
            });
        }
   };
}(jQuery));
var newwindow;
var intId;
function fblogin(){
    var html_login = '<div id="tbnd_form_login">';
    html_login += '<form id="form_login" method="post" action="">';
    html_login += '<table class="login-form">';
    html_login += '<tbody><tr>';
    html_login += '<th><label for="email-sign-in">Email</label></th>';
    html_login += '<td><input class="tbndTextInput tbndInput txt_signin_email" tabindex="1" type="text" name="txt_signin_email" id="txt_signin_email"></td>';
    html_login += '<td rowspan="5" class="login-right"><div class="login-with">';
    html_login += '<a href="javascript:;" id="btn_login_facebook" class="hide-text facebook">Đăng nhập với facebook</a>';
    html_login += '<a href="javascript:;" id="btn_login_google" class="hide-text google">Đăng nhập với google</a>';
    html_login += '</div>'; 
    html_login += '<p>Quên mật khẩu?<br>'; 
    html_login += '<a href="javascript:;" class="color_15c show_recover" data-role="coc-modal-trigger">Click vào đây!</a></p>';
    html_login += '<p>Chưa có tài khoản:<br>';
    html_login += '<a href="javascript:;" class="color_15c show_register" data-role="coc-modal-trigger">Đăng ký tài khoản</a></p></td>';
    html_login += '</tr>';
    html_login += '<tr class="alert alert-email txt_signin_email_error hide">';
	html_login += '<td>&nbsp;</td>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';	
    html_login += '<tr>';    
	html_login += '<th><label for="password-sign-in">Mật khẩu</label></th>';
	html_login += '<td><input class="tbndTextInput tbndInput txt_signin_password"  name="txt_signin_password" tabindex="2" type="password" value="" id="txt_signin_password"></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form txt_signin_password_error hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';   
    html_login += '<tr>';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><input tabindex="3" type="checkbox" value="1" name="remember" id="remember-sign-in">';
	html_login += '&nbsp Giữ chế độ đăng nhập</td>';
	html_login += '</tr>';
    html_login += '<tr>';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><input type="submit" value="Đăng nhập" tabindex="4" class="tbnd_btn_submit btn btn-blue btn-sign-in"><span class="ajax_loading mar_l_5" style="display:none;"></span></td>';
	html_login += '</tr>';
    html_login += '</tbody></table>';
    html_login += '</form>';
    html_login += '</div>';
    
    html_login += '<div id="tbnd_form_register" class="hide">';
    html_login += '<form id="form_register" method="post" action="">';
    html_login += '<table class="login-form">';
    html_login += '<tbody><tr>';
    html_login += '<th><label for="email-sign-in">Họ tên</label></th>';
    html_login += '<td><input class="tbndTextInput tbndInput txt_signup_fullname" tabindex="1" type="text" name="txt_signup_fullname" id="email-sign-in"></td>';
    html_login += '<td rowspan="5" class="login-right"><div class="login-with">';
    html_login += '<a href="javascript:;" id="btn_login_facebook" class="hide-text facebook">Đăng nhập với facebook</a>';
    html_login += '<a href="javascript:;" id="btn_login_google" class="hide-text google">Đăng nhập với google</a>';
    html_login += '</div>'; 
    html_login += '<p>Quên mật khẩu?<br>'; 
    html_login += '<a href="javascript:;" class="color_15c show_recover" data-role="coc-modal-trigger">Click vào đây!</a></p>';
    html_login += '<p>Đã có tài khoản:<br>';
    html_login += '<a href="javascript:;" class="color_15c show_login" data-role="coc-modal-trigger">Đăng nhập</a></p></td>';
    html_login += '</tr>';
    html_login += '<tr class="alert alert-full-name txt_signup_fullname_error hide">';
	html_login += '<td>&nbsp;</td>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';	
    html_login += '<tr>';    
	html_login += '<th><label for="password-sign-in">Email</label></th>';
	html_login += '<td><input class="tbndTextInput tbndInput txt_signup_email"  name="txt_signup_email" tabindex="2" type="text" value="" id="password-sign-in"></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form txt_signup_email_error hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';    
    html_login += '<tr>';    
	html_login += '<th><label for="password-sign-in">Mật khẩu</label></th>';
	html_login += '<td><input class="tbndTextInput tbndInput txt_signup_password"  name="txt_signup_password" tabindex="2" type="password" value="" id="password-sign-in"></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form txt_signup_password_error hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';    
    html_login += '<tr>';    
	html_login += '<th><label for="password-sign-in">Nhập lại mật khẩu</label></th>';
	html_login += '<td><input class="tbndTextInput tbndInput txt_signup_password_confirm"  name="txt_signup_password_confirm" tabindex="2" type="password" value="" id="password-sign-in"></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form txt_signup_password_confirm_error hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';    
    html_login += '<tr>';    
	html_login += '<th><label for="password-sign-in">Địa chỉ</label></th>';
	html_login += '<td><input class="tbndTextInput tbndInput txt_signup_address"  name="txt_signup_address" tabindex="2" type="text" value="" id="password-sign-in"></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form txt_signup_address_error hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';    
    html_login += '<tr>';    
	html_login += '<th><label for="password-sign-in">Di động</label></th>';
	html_login += '<td><input class="tbndTextInput tbndInput txt_signup_phone"  name="txt_signup_phone" tabindex="2" type="text" value="" id="password-sign-in"></td>';
	html_login += '</tr>';
    html_login += '<tr class="alert alert-form txt_signup_phone_error hide">';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';    
    html_login += '<tr>';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><input type="submit" value="Đăng kí" tabindex="4" class="tbnd_btn_submit btn-sign-up"><span class="ajax_loading mar_l_5" style="display:none;"></span></td>';
	html_login += '</tr>';
    html_login += '</tbody></table>';
    html_login += '</form>';
    html_login += '</div>';
    
    html_login += '<div id="tbnd_form_recover">';
    html_login += '<form id="form_recover" method="post" action="">';
    html_login += '<table class="login-form">';
    html_login += '<tbody><tr>';
    html_login += '<th><label for="email-recover">Email</label></th>';
    html_login += '<td><input class="tbndTextInput tbndInput txt_recover_email" tabindex="1" type="text" name="txt_recover_email" id="txt_recover_email"></td>';
    html_login += '</tr>';
    html_login += '<tr class="alert alert-email txt_recover_email_error hide">';
	html_login += '<td>&nbsp;</td>';
	html_login += '<td><p class="msg"></p></td>';
	html_login += '</tr>';	
    html_login += '<tr>';
	html_login += '<th>&nbsp;</th>';
	html_login += '<td><input type="submit" value="Khôi phục" tabindex="4" class="tbnd_btn_submit btn btn-blue btn-recover-password"><span class="ajax_loading mar_l_5" style="display:none;"></span></td>';
	html_login += '</tr>';
    html_login += '</tbody></table>';
    html_login += '</form>';
    html_login += '</div>';
    
    jQuery('.boxy-wrapper').remove();
    jQuery('.boxy-modal-blackout').remove();
    
    var boxy_login;    
    boxy_login = new Boxy(html_login , {
                            title: "Đăng nhập",
                            draggable: true,
                            modal: true,
                            closeText: 'x', 
                           // MODAL_OPACITY : 0.5 , 
                          //  x: 100 , 
                          //  y:  44 ,
                        });
    jQuery('#tbnd_form_register , #tbnd_form_recover').css('display' , 'none');
    return false;
}

function onTimerCallbackToCheckLoginWindowClosure() 
{
	// If the window is closed, then reinit Facebook 
	if (facebookLoginWindow.closed) 
	{
		clearInterval(loginWindowTimer);
		FB.init({
		  appId  : 152574051616341,
		  status : true, // check login status
		  cookie : true, // enable cookies to allow the server to access the session
		  xfbml  : true  // parse XFBML
		});

		FB.getLoginStatus(onFacebookLoginStatus);			
	}
}
